package application.handlers;

import application.RequestHandler;
import contracts.requests.RegisterRequest;
import contracts.responses.RegisterResponse;
import domain.services.UserDomainService;
import eventbus.PageEventBus;
import eventbus.PageEvents;
import internal.InternalResult;
import io.netty.channel.Channel;
import util.ChannelHelper;

/**
 * 注册处理器
 *
 * @author wyl
 */
public class RegisterHandler extends RequestHandler<RegisterRequest, RegisterResponse> {
    @Override
    public RegisterResponse handle(RegisterRequest registerRequest, Channel requestChannel) {
        InternalResult result = UserDomainService.Instance.register(
                registerRequest.username,
                registerRequest.password,
                registerRequest.nickname);

        //注册成功通知界面刷新
        if (result.isSuccess()) {
            PageEventBus.publish(PageEvents.USER_REGISTER_EVENT);
        }


        return new RegisterResponse(
                result.isSuccess(),
                ChannelHelper.getConnId(requestChannel),
                result.getErrorMessage());
    }
}
